package cc.comac.mom.webservice.impl;

import java.util.ArrayList;
import java.util.Date;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.ans.shaded.com.alibaba.fastjson.JSONArray;
import com.alibaba.ans.shaded.com.alibaba.fastjson.JSONObject;

import cc.comac.mom.bean.QualityResultBean;
import cc.comac.mom.datahandlers.entity.QualityQarVerifyPlanDataHandler;
import cc.comac.mom.entity.QualityQarVerifyPlanEntity;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.webservice.QualityQarVerifyPlanWebService;

@WebService(serviceName = "QarVerifyPlan", targetNamespace = "http://webservice.mom.comac.cc/", endpointInterface = "cc.comac.mom.webservice.QualityQarVerifyPlanWebService")
@Component
public class QualityQarVerifyPlanWebServiceImpl implements QualityQarVerifyPlanWebService{

	@Autowired
	private QualityQarVerifyPlanDataHandler  qualityQarVerifyPlanDataHandler ;
	@Override
	
	//保存检验计划
	public String sendCorrectiveActionPlanToQar(String json) {
		QualityResultBean qualityResultBean = new QualityResultBean();
		JSONObject parseObject = JSONObject.parseObject(json.toString());
		String project = parseObject.getString("project");
		String sid = parseObject.getString("sid");
		String assignerName = parseObject.getString("assignerName");
		Date assignTime = parseObject.getDate("assignTime");
		JSONArray jsonArray = parseObject.getJSONArray("qar");
		if (jsonArray == null || jsonArray.size() < 1) {
			qualityResultBean.setMsg("qar信息为空");
			qualityResultBean.setStatus("0");
			return qualityResultBean.toString();
		}
		
		ArrayList<QualityQarVerifyPlanEntity> listEntity = new ArrayList<QualityQarVerifyPlanEntity>();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			String partno = jsonObject.getString("partno");
			Integer copyNumber = Integer.valueOf(jsonObject.getString("copy-number"));
			QualityQarVerifyPlanEntity qualityQarVerifyPlanEntity = new QualityQarVerifyPlanEntity();
			qualityQarVerifyPlanEntity.setProject(project);
			qualityQarVerifyPlanEntity.setAssignerName(assignerName);
			qualityQarVerifyPlanEntity.setAssignTime(assignTime);
			qualityQarVerifyPlanEntity.setSid(sid);
			qualityQarVerifyPlanEntity.setPartno(partno);
			qualityQarVerifyPlanEntity.setCopyNumber(copyNumber);
			listEntity.add(qualityQarVerifyPlanEntity);
		}
		ResultBean resultBean = qualityQarVerifyPlanDataHandler.insertBatch(listEntity);
		if (!resultBean.success) {
			qualityResultBean.setMsg(resultBean.getMessage());
			qualityResultBean.setStatus("0");
			return qualityResultBean.toString();
		}
		qualityResultBean.setMsg("成功");
		qualityResultBean.setStatus("1");
		return qualityResultBean.toString();
	}

	//删除检验计划
	@Override
	public String cancelCorrectiveAction(String json) {
		QualityResultBean qualityResultBean = new QualityResultBean();
		JSONObject parseObject = JSONObject.parseObject(json.toString());
		String project = parseObject.getString("project");
		String sid = parseObject.getString("sid");
		String invokerName = parseObject.getString("invokerName");
		Date invokeDate = parseObject.getDate("invokeDate");
		QualityQarVerifyPlanEntity qualityQarVerifyPlanEntity = new QualityQarVerifyPlanEntity();
		
		qualityQarVerifyPlanEntity.setProject(project);
		qualityQarVerifyPlanEntity.setSid(sid);
		qualityQarVerifyPlanEntity.setInvokerName(invokerName);
		qualityQarVerifyPlanEntity.setInvokeDate(invokeDate);
		ResultBean resultBean = qualityQarVerifyPlanDataHandler.deleteBySid(qualityQarVerifyPlanEntity);
		if (!resultBean.success) {
			qualityResultBean.setMsg(resultBean.getMessage());
			qualityResultBean.setStatus("0");
			return qualityResultBean.toString();
		}
		qualityResultBean.setMsg("成功");
		qualityResultBean.setStatus("1");
		return qualityResultBean.toString();
	}

}
